﻿using Data;
using Login.Server;
using LoginUI.Tip;
using LoginUI.UI;
using UnityEngine.UI;

namespace LoginUI.Login
{
    public class LoginPanel : BasePanel
    {
        // 确定登录按钮
        public Button btnLogin;

        // 注册按钮
        public Button btnRegister;

        // 账号输入框
        public InputField inputAccount;

        public InputField inputPassword;

        // 记住密码
        public Toggle toggleRemember;

        // 自动登录
        public Toggle toggleAuto;

        // 登录数据
        private LoginData _loginData;

        public override void Init()
        {
            // 登录按钮
            btnLogin.onClick.AddListener(OnLoginClick);
            // 注册按钮
            btnRegister.onClick.AddListener(OnRegisterClick);
            // 记住密码
            toggleRemember.onValueChanged.AddListener(OnRememberClick);
            // 自动登录
            toggleAuto.onValueChanged.AddListener(OnAutoClick);
            // 账号输入框
            inputAccount.onValueChanged.AddListener(OnAccountChange);
            // 密码输入框
            inputPassword.onValueChanged.AddListener(OnPasswordChange);
            if (_loginData.isAuto) InvokerLogin(false);
        }

        // 登录按钮点击事件
        private void OnLoginClick()
        {
            InvokerLogin();
        }

        private void InvokerLogin(bool isFade = true)
        {
            // 登录校验
            // _loginData.password _loginData.account 合法性校验
            if (_loginData.account == "" || _loginData.password == "")
            {
                var tipInfo = "账号或密码不能为空";
                var tipPanel = UIManager.Instance.ShowPanel<TipPanel>();
                tipPanel.ChangeInfo(tipInfo);
                return;
            }

            if (!validate.ValidateAccount(_loginData.account) || !validate.ValidatePassword(_loginData.password))
            {
                var tipInfo = "账号或密码不合法";
                var tipPanel = UIManager.Instance.ShowPanel<TipPanel>();
                tipPanel.ChangeInfo(tipInfo);
                return;
            }

            var registerData = LoginMgr.Instance.RegisterData;
            // 检查账号是否存在
            if (!registerData.registerInfo.ContainsKey(_loginData.account))
            {
                var tipInfo = "账号不存在";
                var tipPanel = UIManager.Instance.ShowPanel<TipPanel>();
                tipPanel.ChangeInfo(tipInfo);
                return;
            }

            // 检查密码是否正确
            if (registerData.registerInfo[_loginData.account] != _loginData.password)
            {
                var tipInfo = "密码错误";
                var tipPanel = UIManager.Instance.ShowPanel<TipPanel>();
                tipPanel.ChangeInfo(tipInfo);
                return;
            }

            // 登录成功
            else
            {
                UIManager.Instance.HidePanel<LoginPanel>(isFade);
                // 检查该账号是否上次选择过服务器
                if (LoginMgr.Instance.LoginData.lastServerId >= 0)
                    // 直接进入ServerPanel
                    UIManager.Instance.ShowPanel<ServerPanel>();
                else
                    // 进入ChooseServerPanel
                    UIManager.Instance.ShowPanel<ChooseServerPanel>();
            }
        }

        // 注册按钮点击事件
        private void OnRegisterClick()
        {
            // 显示注册面板
            UIManager.Instance.ShowPanel<RegisterPanel>();
            // 隐藏登录面板
            UIManager.Instance.HidePanel<LoginPanel>();
        }

        private void OnRememberClick(bool isOn)
        {
            _loginData.isRemember = isOn;
            JsonMgr.Instance.SaveData(_loginData, "LoginData");
        }

        private void OnAutoClick(bool isOn)
        {
            _loginData.isAuto = isOn;
            if (isOn) toggleRemember.isOn = true;
            // _loginData.isRemember = true;
            JsonMgr.Instance.SaveData(_loginData, "LoginData");
        }

        private void OnAccountChange(string UN)
        {
            _loginData.account = UN;
            JsonMgr.Instance.SaveData(_loginData, "LoginData");
        }

        private void OnPasswordChange(string PW)
        {
            _loginData.password = PW.ToString();
            JsonMgr.Instance.SaveData(_loginData, "LoginData");
        }

        public override void ShowMe()
        {
            base.ShowMe();
            // LoginMgr中获取数据
            _loginData = LoginMgr.Instance.LoginData;
            inputAccount.text = _loginData.account;
            inputPassword.text = _loginData.password;
            toggleAuto.isOn = _loginData.isAuto;
            toggleRemember.isOn = _loginData.isRemember;
        }
    }
}